package com.lx.thread.MyThread.lx;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-04-18 23:00:31
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-04-18     ������                     1.0         1.0 Version
 */
public class SemaphoreTest {
    public volatile   static List<Integer> list= Collections.synchronizedList(new ArrayList<Integer>());

    public static Object object=new Object();
    public final static Semaphore semaphore=new Semaphore(4);


    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->yewu()).start();
        }
    }

    public static void yewu()  {
        try {

            semaphore.acquire();
            Thread.sleep(1000);
            Random random = new Random(10000);
            int i = random.nextInt();
            list.add(i);


        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            synchronized (object) {
                outSize(list);
                semaphore.release();
                reloadCountDownLatch();

            }
        }
    }
    public synchronized static void reloadCountDownLatch(){
             list.clear();
    }

    /**
     * <p>
     *    ���list�ĳ���
     * </p>
     * @param  list
     * @return void
     * @Author zlx
     * @date 2021/4/18
     */
    public static void outSize(List list){
        System.out.println("list size print " +list.size());
    }
}